package com.yxtp.controller;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.ModelAndView;

import com.yxtp.entity.ImgInfo;
import com.yxtp.service.ImgService;
import com.yxtp.util.FileUploadUtils;
import com.yxtp.util.FileUploadUtils;

/**
 * 图片处理类
 * 
 * @author Nicky
 * @date 2019年5月28日
 */
@Controller
@RequestMapping("/upload")
public class ImgController {

	@Autowired
	private ImgService imgService;

	/*
	 * 文件上传方法2 
	 * Tomcat虚拟服务器路径/img
	 * 回显图片<img src="/img/${imginfo.imgPath}" />
	 */
	@RequestMapping("/uploadAction")
	public void uploadAction(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 创建一个通用的多部分解析器
		CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
				request.getSession().getServletContext());
		// 判断request是否有文件上传
		if (multipartResolver.isMultipart(request)) {
			// 转换成多部分request
			MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
			// 使用迭代器取得request中所有的文件名
			Iterator<String> iter = multiRequest.getFileNames();
			while (iter.hasNext()) {
				// 取得上传文件
				MultipartFile file = multiRequest.getFile(iter.next());
				// 取得上传文件的文件名称
				String fileName = file.getOriginalFilename();
				// 保存本地文件
				String imgPath = new FileUploadUtils().getImgPath(file);
				// 获取用户信息并保存至数据库
				String username = request.getParameter("username");
				String id = request.getParameter("uid");
				int uid = Integer.parseInt(id);
				// 上传日期
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				if (imgPath != null) {
					ImgInfo imginfo = new ImgInfo();
					imginfo.setUid(uid);
					imginfo.setUsername(username);
					imginfo.setDate(sdf.format(new Date()));
					imginfo.setImgPath(imgPath);
					imgService.addImgInfo(imginfo);
					System.out.println(imginfo);
					System.out.println("上传成功" + imgPath);
				} else {
					System.out.println("上传失败");
				}
			}
		}
	}

	/*
	 * 实现文件上传,图片保存本地,图片的相对路径保存数据库，预先设置上传文件夹路径的Tomcat虚拟服务器路径为/img
	 */
	@RequestMapping("/uploadPhoto")
	public String upload(@RequestParam(value = "username") String username,
			@RequestParam(value = "description", required = false) String description, Model model,
			@RequestParam(value = "file") MultipartFile file, HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		String imgPath = new FileUploadUtils().getImgPath(file);
		ImgInfo imginfo = new ImgInfo();
		if (imginfo != null) {
			imginfo.setUsername(username);
			imginfo.setDescription(description);
			imginfo.setImgPath(imgPath);
			imginfo.setFile(file);
			System.out.println(imginfo);
			imgService.addImgInfo(imginfo);
		}
		model.addAttribute("imginfo", imginfo);
		return "/testupload.jsp?";
	}

	/*
	 * 查询图片
	 */
	@RequestMapping("/showPhoto")
	public String showPhoto(Model model, HttpServletRequest request, HttpServletResponse response) {
		List<ImgInfo> infoList = this.imgService.selectAllImgPath();
		model.addAttribute("infoList", infoList);
		return "testPhotoList.jsp";
	}

}
